package com.chaofan.authsso.client.config;

import com.chaofan.authsso.client.common.Constant;
import com.chaofan.authsso.client.filter.LoginCheckFilter;
import com.chaofan.authsso.client.properties.SsoRegisterProperties;
import com.chaofan.authsso.client.util.TokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * Author: Chaofan
 * Date: 2020/6/9 16:38
 * email: chaofan2685@qq.com
 */
public class BeanConfig {

    public static final Logger logger = LoggerFactory.getLogger(BeanConfig.class);

    @Autowired
    SsoRegisterProperties ssoRegisterProperties;

    @Bean
    public SsoRegisterProperties ssoRegisterPropertiesRegister(){
        return new SsoRegisterProperties();
    }

    @Bean
    public FilterRegistrationBean outLinkSecurityFilterRegister() {
        System.out.println("┌─┐┬ ┬┌┬┐┬ ┬   ┌─┐┌─┐┌─┐");
        System.out.println("├─┤│ │ │ ├─┤───└─┐└─┐│ │");
        System.out.println("┴ ┴└─┘ ┴ ┴ ┴   └─┘└─┘└─┘");
        if (TokenUtil.checkCoreService(ssoRegisterProperties.getHost())){
            logger.info(Constant.AUTH_SSO_SERVICE_CONNECTION_SUCCEEDED);
        }else {
            logger.error(Constant.AUTH_SSO_SERVICE_CONNECTION_FAILED);
        }
        FilterRegistrationBean registration = new FilterRegistrationBean();
        //注入过滤器
        registration.setFilter(new LoginCheckFilter(ssoRegisterProperties));
        //拦截规则
        registration.addUrlPatterns(ssoRegisterProperties.getLoginCheck().toArray(new String[ssoRegisterProperties.getLoginCheck().size()]));
        //过滤器名称
        registration.setName(Constant.AUTH_SSO_FILTER_NAME_STR);
        //过滤器顺序
        registration.setOrder(1);

        return registration;
    }

}
